package com.base.cn.platform.os.controller.course.cloud;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.entity.manage.config.SysConfig;
import com.base.cn.platform.os.service.course.cloud.CloudService;
import com.base.cn.platform.os.service.manage.config.SysConfigService;
import com.base.cn.platform.os.service.manage.user.sms.UserSendSmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Zhanglei
 * @create 2018-08-21 下午 3:09
 */
@Controller
@RequestMapping("/admin/course")
public class AdminCloudController extends AdminBaseController {

    @Autowired
    private CloudService cloudService;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private UserSendSmsService userSendSmsService;

    /**
     * 选择视频、音频
     * @return 操作页面
     */
    @RequestMapping("/selectVideo")
    public String selectVideo(@RequestParam("fileType") String fileType,Model model){
        model.addAttribute("fileType",fileType);
        return resultPage("/course/cloud/select_demand_index");
    }

    /**
     * 加载音频、视频数据列表
     * @return 操作页面
     */
    @RequestMapping("/ajaxSelectVideo")
    public String ajaxSelectVideo(Model model,
                                  @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                                  @RequestBody Map<String, Object> params){
        Map<String,Object> dataMap = cloudService.findDemandPage(currentPage, params);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/course/cloud/ajax_select_demand");
    }

    //==================================================

    /**
     * 批量上传
     * @param model
     * @return 操作页面
     */
    @RequestMapping("/batchUpload")
    public String batchUpload(Model model){
        TreeMap<String,String> configMap = sysConfigService.getSysConfigContext(ConfigName.V96K);
        model.addAttribute("configMap",configMap);
        TreeMap<String,String> vlasProviderMap = sysConfigService.getSysConfigContext(ConfigName.VLASPROVIDER);
        model.addAttribute("vlasProviderMap",vlasProviderMap);
        return resultPage("/course/cloud/batch-upload-video");
    }

    /**
     * 录播首页
     */
    @RequestMapping("/demand/toDemandIndex")
    public String toDemandIndex(Model model)
    {
        validateOpenCloudServices(false,model,2);
        return resultPage("/course/cloud/demand_index");
    }

    /**
     * 录播
     *
     * @param currentPage 当前页
     * params 录播数据
     */
    @RequestMapping("/ajaxDemandList")
    public String ajaxDemandList(Model model,
                                 @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                                 @RequestBody Map<String, Object> params) {
        Map<String,Object> dataMap = cloudService.findDemandPage(currentPage, params);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/course/cloud/ajax_demand");
    }

    /**
     * 直播首页
     */
    @RequestMapping("/live/toLiveIndex")
    public String toLiveIndex(Model model)
    {
        TreeMap<String,String> vlasProviderMap = sysConfigService.getSysConfigContext(ConfigName.VLASPROVIDER);
        model.addAttribute("vlasProviderMap",vlasProviderMap);
        validateOpenCloudServices(false,model,3);
        if(vlasProviderMap.get("type").equals("CC")){
            return resultPage("/course/cloud/cclive/live_index");
        }else{
            return resultPage("/course/cloud/ttlive/live_index");
        }
    }

    /**
     * 直播
     *
     * @param currentPage 当前页
     * @param params 直播数据
     */
    @RequestMapping("/ajaxLiveList")
    public String ajaxLiveList(HttpServletRequest request,Model model,
                               @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                               @RequestBody Map<String, Object> params) {
        String domain =request.getScheme()+"://"+ request.getServerName();
        TreeMap<String,String> vlasProviderMap = sysConfigService.getSysConfigContext(ConfigName.VLASPROVIDER);
        if(vlasProviderMap.get("type").equals("CC")){ //cc视频
            Map<String,Object> dataMap = cloudService.findCCLivePage(currentPage);
            ArrayList<LinkedHashMap> list = (ArrayList)dataMap.get("list");
            if(ObjectUtils.isNotEmpty(list)){
                list.stream().forEach(l->{
                    l.put("shareUrl",""+domain+"/admin/toLiveByLink/toLiveByLinkByCC?playurl="+l.get("id")+"&admincode="+l.get("assistantPass")+"&ptcode="+l.get("playPass")+"&teachercode="+l.get("publisherPass")+"type=ccLive");
                });
            }
            model.addAttribute("dataMap",dataMap);
            return resultPage("/course/cloud/cclive/ajax_live");
        }else{ //3t视频
            Map<String,Object> dataMap = cloudService.findTTLivePage(currentPage);
            ArrayList<LinkedHashMap> list = (ArrayList)dataMap.get("list");
            if(ObjectUtils.isNotEmpty(list)){
                SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss");
                list.stream().forEach(l->{
                    l.put("beginTime",sdf.format(new Date(Long.valueOf(l.get("beginTime")+"000"))));
                    l.put("endTime",sdf.format(new Date(Long.valueOf(l.get("endTime")+"000"))));
                    l.put("shareUrl",""+domain+"/admin/toLiveByLink/toLiveByLinkByTT?roomId="+l.get("classId")+"&timeMillis="+System.currentTimeMillis()+"type=ttLive");
                });
            }
            model.addAttribute("dataMap",dataMap);
            return resultPage("/course/cloud/ttlive/ajax_live");
        }
    }

    /**
     * 删除录播
     *
     * @param id_varchar 录播播放码
     */
    @RequestMapping("/ajaxDemandDelete")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "删除云服务中上传的文件")
    public Map<String, Object> ajaxDemandDelete(@RequestParam("id_varchar") String id_varchar) {
        return cloudService.deleteDemand(id_varchar);
    }

    /**
     * 关闭直播
     *
     * @param room_id 直播房间id
     */
    @RequestMapping("/ajaxLiveClose")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "关闭直播")
    public Map<String, Object> ajaxLiveClose(@RequestParam("room_id") String room_id,@RequestParam(value = "type",required = false,defaultValue = "0") Integer type) {
        return cloudService.closeLive(room_id,type);
    }

    /**
     * 云服务配置界面
     * @param model
     * @return
     */
    @RequestMapping("/serviceConfig")
    public String ajaxLiveList(Model model) {
        validateOpenCloudServices(true,model,1);
        TreeMap<String,String> vlasProviderMap = sysConfigService.getSysConfigContext(ConfigName.VLASPROVIDER);
        model.addAttribute("vlasProviderMap",vlasProviderMap);
        return resultPage("/course/cloud/service_config");
    }


    /**
     * 获取注册升级云服务验证码
     * mobile
     */
    @RequestMapping("/sendMobileCodeCloud")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "获取注册升级云服务验证码")
    public Map<String, Object> sendMobileCodeCloud(@RequestParam("mobile") String mobile) {
        return   userSendSmsService.sendMobileCode(mobile,6,null);
    }

    /**
     * 开通视频云服务
     *
     * @param params 直播数据
     */
    @RequestMapping("/openVideoCloudService")
    @ResponseBody
    public Map<String,Object> openVideoCloudService(@RequestBody Map<String, String> params) {
        return  cloudService.openVideoCloudService(params);
    }

    /**
     * 验证是否开通了268云服务
     * @return
     */
    private  void validateOpenCloudServices(boolean field,Model model,int type){
        SysConfig sysConfig = sysConfigService.getSysConfigByConfigType(ConfigName.V96K);
        TreeMap<String, String> contextTreeMap =sysConfig.getContextTreeMap();
        if(field){
            model .addAttribute("sysConfig",sysConfig);
        }
        boolean flag =false;
        switch (type){
            case 1:
                if(ObjectUtils.isNotEmpty(contextTreeMap.get("customerKey"))||ObjectUtils.isNotEmpty(contextTreeMap.get("c_auth_id"))||ObjectUtils.isNotEmpty(contextTreeMap.get("p_auth_id"))||ObjectUtils.isNotEmpty(contextTreeMap.get("i_auth_id"))){
                    flag =true;
                }
                if(ObjectUtils.isNotEmpty(contextTreeMap.get("userMobile"))){
                    //验证是否注册了服务
                    model .addAttribute("registrationService",true);
                }else{
                    model .addAttribute("registrationService",false);
                }
                break;
            case 2:
                if(ObjectUtils.isNotEmpty(contextTreeMap.get("customerKey"))){
                    flag =true;
                }
            case 3:
                if(ObjectUtils.isNotEmpty(contextTreeMap.get("c_auth_id"))||ObjectUtils.isNotEmpty(contextTreeMap.get("p_auth_id"))||ObjectUtils.isNotEmpty(contextTreeMap.get("i_auth_id"))){
                    flag =true;

                }
                break;

        }
        model .addAttribute("isOpen",flag);

    }

    /**
     * 打开云视频升级页面
     */
    @RequestMapping("/openUpdateServicePage")
    public String openUpdateServicePage(Model model) {
        validateOpenCloudServices(true,model,0);
        TreeMap<String,String> vlasProviderMap = sysConfigService.getSysConfigContext(ConfigName.VLASPROVIDER);
        model.addAttribute("videoType",vlasProviderMap.get("type"));
        return resultPage("/course/cloud/update_service_config");
    }

    /**
     * 云短信
     */
    @RequestMapping("/cloudSmsIndex")
    public String cloudSmsIndex(Model model) {
        //获取短信服务配置
        SysConfig sysConfig = sysConfigService.getSysConfigByConfigType(ConfigName.SMS);
        model.addAttribute("sysConfig",sysConfig);
        return resultPage("/course/cloud/sms/sms_index");
    }


    /**
     * 获取创世云短信消费记录
     *
     * @param currentPage 当前页
     * @param params 直播数据
     */
    @RequestMapping("/ajaxSmsStatisList")
    public String ajaxSmsStatisList(Model model,
                               @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                               @RequestBody Map<String, Object> params) {
        params.put("page",currentPage);
        Map<String,Object> dataMap = cloudService.querySmsStatis(params,1);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/course/cloud/sms/csy_sms_statis_ajax_list.html");
    }
    /**
     * 获取创世云短信发送记录
     *
     * @param currentPage 当前页
     * @param params 直播数据
     */
    @RequestMapping("/ajaxSmsLog")
    public String ajaxSmsLog(Model model,
                                    @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                                    @RequestBody Map<String, Object> params) {
        params.put("page",currentPage);
        Map<String,Object> dataMap = cloudService.querySmsStatis(params,2);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/course/cloud/sms/csy_sms_log_ajax_list.html");
    }
}
